import sheep from '@/sheep'
import { formatImageUrlProtocol } from './index'

var alipaycode = ''
const alipayACode = async(poster) => {
  const res = await sheep.$api.app.acode.alipay({
    'urlParam': 'pages/index/index',
    'queryParam': poster.shareInfo.query,
    'describe': 'share',
    'color': '0x00BFFF',
    'size': 'm'
  })
  alipaycode = res.data
  console.log('alipayaCode----res---->', res)
}

var wxacode = ''
const weixinACode = async(poster) => {
  const shareParams = `platformName=WechatMiniProgram&${poster.shareInfo.query}`
  const res_params =	await sheep.$api.app.acode.savenShareParams({
    shareParams
  })
  if (res_params.code === '0' && res_params.data) {
    const res =	await sheep.$api.app.acode.weixin({
		  'page': 'pages/index/index',
		  'scene':	'spmid=' + res_params.data,
      'width': 500
    })
    wxacode = 'data:image/jpg;base64,' + res.data
  }
}

const flashSale = async(poster) => {
  const width = poster.width
  const userInfo = sheep.$store('user').userInfo
  const avatar = userInfo.avatarId ? sheep.$url.file(userInfo.avatarId, 'mall-img') : sheep.$url.static('/static/default_avatar.png')
  
  console.log('avatar', avatar)
  // #ifdef MP-WEIXIN
	 await weixinACode(poster)
  // #endif
  // #ifdef MP-ALIPAY
  await alipayACode(poster)
  // #endif
  return {
    background: formatImageUrlProtocol(sheep.$store('app').platform.share.posterInfo.goods_bg),
    list: [
      {
        name: 'nickname',
        type: 'text',
        val: userInfo.nickname,
        maxWidth: width * 0.73,
        line: 1,
        x: width * 0.22,
        y: width * 0.06,
        paintbrushProps: {
          fillStyle: '#333',
          font: {
            fontSize: 16,
            fontFamily: 'sans-serif'
          }
        }
      },
      {
        name: 'avatar',
        type: 'image',
        val: formatImageUrlProtocol(avatar),
        x: width * 0.04,
        y: width * 0.04,
        width: width * 0.14,
        height: width * 0.14,
        d: width * 0.14
      },
      {
        name: 'goodsImage',
        type: 'image',
				  val: formatImageUrlProtocol(poster.shareInfo.poster.image),
        x: width * 0.03,
        y: width * 0.21,
        width: width * 0.94,
        height: width * 0.94,
        r: 10
      },
      {
        name: 'goodsTitle',
        type: 'text',
        val: poster.shareInfo.poster.title,
        x: width * 0.04,
        y: width * 1.18,
        maxWidth: width * 0.91,
        line: 2,
        lineHeight: 5,
        paintbrushProps: {
          fillStyle: '#333',
          font: {
            fontSize: 14
          }
        }
      },
      {
			  name: 'goodsOriginalPrice',
			  type: 'text',
			  val:
			   '限时抢购',
			  x: width * 0.03,
			  y: width * 1.31,
			  paintbrushProps: {
			    fillStyle: '#ff0000',
			    font: {
			      fontSize: 16,
			      fontFamily: 'OPPOSANS'
			    }
			  }
      },
      {
        name: 'goodsPrice',
        type: 'text',
        val: '￥' + poster.shareInfo.poster.price,
        x: width * 0.24,
        y: width * 1.3,
        paintbrushProps: {
          fillStyle: '#ff0000',
          font: {
            fontSize: 20,
            fontFamily: 'OPPOSANS'
          }
        }
      },
      // #ifdef H5
      {
        name: 'qrcode',
        type: 'qrcode',
        val: poster.shareInfo.link,
        x: width * 0.75,
        y: width * 1.3,
        size: width * 0.2
      },
      // #endif
      // #ifdef MP-ALIPAY
      {
        name: 'wxacode',
        type: 'image',
        val: alipaycode,
        x: width * 0.75,
        y: width * 1.3,
        width: width * 0.2,
        height: width * 0.2
      },
      // #endif
      // #ifdef MP-WEIXIN
      {
        name: 'wxacode',
        type: 'image',
        val: wxacode,
        x: width * 0.75,
        y: width * 1.3,
        width: width * 0.2,
        height: width * 0.2
      }
      // #endif
    ]
  }
}

export default flashSale
